//
//  SignViewController.m
//  SignatrueForPdf
//
//  Created by glendale on 13-8-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "SignViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "UIImage+MyImage.h"

@interface SignViewController ()
//给当前的view创建一个subview，并添加
-(void)createDrawView;
@end

@implementation SignViewController

@synthesize paletteIns = _paletteIns;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createDrawView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)createDrawView
{
    CGRect rect = CGRectMake(0, 0, 1024, 700);
    self.paletteIns = [[MyPalette alloc] initWithFrame:rect];
    
    //设置背景色
   // self.paletteIns.backgroundColor = [UIColor whiteColor];
 
    [self.view insertSubview:self.paletteIns atIndex:0];
    //[self.view addSubview:self.paletteIns];
}

#pragma mark - 按钮的回调函数

- (IBAction)rewriteButtonPressed:(UIButton *)sender
{
    NSLog(@"rewriteButtonPressed...");
    
    //处理按键－－－清除签名，即清空所有的数据。
    
    [self.paletteIns shanchu];
    
    [self.paletteIns setNeedsDisplay];
}

- (IBAction)submitButtonPressed:(UIButton *)sender
{
    NSLog(@"submitButtonPressed...");
    
    //处理按键－－－提交按钮，此处表示签名完成，以下就是完成后的工作。
  
 
    [self SignSavePDf];
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)SignSavePDf{
    CGSize s = self.paletteIns.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    [self.paletteIns.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if (self.returnImage) {
        self.returnImage(image);
    }
//    CGPDFDocumentRef pdf;
//
//
//    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//    NSURL *url = [NSURL fileURLWithPath:path];
//   // CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("test.pdf"), NULL, NULL);
//    CFURLRef pdfURL = CFBundleCopyResourceURLInDirectory((CFURLRef)(__bridge void *)(url), CFSTR("print.pdf"), nil, nil);
//    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
////    CFRelease(pdfURL);
//
//
//
//    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"print.pdf"];
//
//
//    size_t count = CGPDFDocumentGetNumberOfPages (pdf);// 3
//    if (count == 0) {
//        NSLog(@"PDF needs at least one page");
//        return;
//    }
//
//
//    CGRect paperSize = CGRectMake(0.0,0.0,595,841);
//
//
//    UIGraphicsBeginPDFContextToFile( fullPath, paperSize, nil);//将pdf写如文件
//
//    for (int i=1; i<=count; i++)
//    {
//        UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
//        CGContextRef currentContext = UIGraphicsGetCurrentContext();
//        CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
//        CGContextScaleCTM(currentContext, 1.0, -1.0);
//        CGPDFPageRef page = CGPDFDocumentGetPage (pdf, i);
//        CGContextDrawPDFPage (currentContext, page);
//
//        if (1 == i)
//        {
//            UIImage *image = [UIImage imageFromView:_paletteIns];
//
//            CGImageRef pageImage = [image CGImage];
//            CGContextDrawImage(currentContext, CGRectMake(500,760,60 , 25), pageImage);//绘制图片
//        }
//    }
//
////    UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
////
////    CGContextRef currentContext = UIGraphicsGetCurrentContext(); // flip context so page is right way up
////    CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
////    CGContextScaleCTM(currentContext, 1.0, -1.0);
////    CGPDFPageRef page = CGPDFDocumentGetPage (pdf, 1); // grab page 1 of the PDF
////    CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context // flip context so annotations are right way up
////
////    if (count > 1)
////    {
////        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 595, 841), nil);
////        CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
////        CGContextScaleCTM(currentContext, 1.0, -1.0);
////        CGPDFPageRef page2 = CGPDFDocumentGetPage (pdf, 2); // grab page 1 of the PDF
////        CGContextDrawPDFPage (currentContext, page2); // draw page 1 into graphics context // flip context so annotations are right way up
////    }
//
//
//    UIGraphicsEndPDFContext();
//
//
//    CGPDFDocumentRelease (pdf);
//
}

//


@end
